﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VipSys.DataLayer;
using VipSys.SystemEntity;
using VipSys.Common.Library;
namespace VipSys
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                switch (action)
                {
                    case "check":
                        Check();
                        return;
                    default:
                        return;
                }
            }
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            log4net.Config.XmlConfigurator.Configure();
        }
        private void Check()
        {
            //SysAccount sysAccount = Session["SysAccount"] as SysAccount;
            string jsons = "";
            SysAccount account = new SysAccount();
            var Name = Request["name"];
            var Password = Request["password"];
            var role = Request["role"];
            account.Name = Name;
            account.Password = Password;
            account.RoleType = role;
            if (role == SystemData.LoginType.User)
            {
                User user=new User();
                User query = new User();
                query.Tel = Name;
                query.PassWord = Password;
                short shRet = UserAccess.Instance.GetUser(query, ref user);

                if (shRet == SystemData.ReturnValue.OK)
                {
                    account.ID = user.ID;
                    account.Name = user.Name;
                    account.RoleType = "会员";
                    account.Room = user.Room;
                    account.Password = user.PassWord;
                    account.Hotel = user.Hotel;
                    Session["SysAccount"] = account;
                    jsons = "{\"success\":true,msg:\"登陆成功\"}";
                }
                else
                {
                    jsons = "{\"success\":true,msg:\"登陆失败\"}";
                }
               
            }
            else if(role==SystemData.LoginType.Employee)
            {
                List<Employee> lstEmployee = new List<Employee>();
                Employee employee = new Employee();
                Employee query =new Employee();
                query.EmpNo = account.Name;
                query.PassWord = account.Password;
                short shRet= EmployeeAccess.Instance.GetEmployee(query, ref employee);
                if (shRet == SystemData.ReturnValue.OK)
                {
                    account.ID = employee.ID;
                    account.RoleType = employee.RoleType;
                    account.OrgName = employee.OrgName;
                    account.Password = employee.PassWord;
                    Session["SysAccount"] = account;
                    jsons = "{\"success\":true,msg:\"登陆成功\"}";
                    SystemParam.Instance.Log.Info(string.Format("系统使用者账号：{0} 姓名：{1}", employee.EmpNo, employee.Name));
                }
                else {
                    jsons = "{\"success\":true,msg:\"登陆失败\"}";
                }
            }
            this.Response.Clear();
            this.Response.Write(jsons);
            this.Response.End();
        }
    }
}